public without sharing class ContractProductSyncHandler {
    ///在新建或者更新合同产品的时候 将 合同产品中产品的价格 同步到合同产品的价格
    public static void syncPrice2ContractProduct(List<ContractProduct__c> cps){
        Set<Id> conids = new Set<Id>();
        Set<Id> prdids = new Set<Id>();
        for (ContractProduct__c cp : cps ){
          conids.add(cp.Contract__c);
          prdids.add(cp.Product__c);
        }

        Map<Id, Contract__c> conMap = new Map<Id, Contract__c>();
        for (Contract__c con : [select id, CompanyControls__c from Contract__c where id in: conids]) {
          conMap.put(con.Id, con);
        }
       
        Map<String, Price__c> prMap = new Map<String, Price__c>();
        for (Price__c pr : [select Product__c, Company__c, LastMonthAveragePrice__c , HospitalBottomPrice__c ,CompanyBottomPrice__c 
                from Price__c where Product__c in :prdids]) {
          prMap.put(pr.Product__c + pr.Company__c, pr);
        }

        for (ContractProduct__c cp : cps ){
            if(conMap.containsKey(cp.Contract__c)){
        	   String company = conMap.get(cp.Contract__c).CompanyControls__c;
        	   String product = cp.Product__c;
         	   String key = product+company;
         	   if (prMap.containsKey(key)) {
         	     cp.LastMonthAveragePrice__c = prMap.get(key).LastMonthAveragePrice__c;
         	     cp.HospitalBottomPrice__c = prMap.get(key).HospitalBottomPrice__c;
         	     cp.CompanyBottomPrice__c = prMap.get(key).CompanyBottomPrice__c;
         	    
        		 }
      		 }            
         }
        
    } 
}